﻿using Api.Application.Command.Role;
using Api.DTO;
using AutoMapper;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.Role
{
    public class ShowRoleHandler : IRequestHandler<ShowRoleCommand, ApiPaging<RoleDTO>>
    {
        private readonly IBase<RoleModel> roleRepo;
        private readonly IMapper mapper;

        public ShowRoleHandler(IBase<RoleModel> roleRepo,IMapper mapper)
        {
            this.roleRepo = roleRepo;
            this.mapper = mapper;
        }
        public Task<ApiPaging<RoleDTO>> Handle(ShowRoleCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<RoleDTO> paging = new ApiPaging<RoleDTO>();
            var role = roleRepo.GetAll().Where(x => x.IsDeleted == false);
            var linq = from r in role
                       select new RoleDTO
                       {
                           Id=r.Id,
                           RoleName = r.RoleName,
                           CrearName = r.CrearName,
                           CrearTime = r.CrearTime,
                           UpdateName = r.UpdateName,
                           UpdateTime = r.UpdateTime,
                           
                       };
            paging.TotalCount = linq.Count();
            paging.Data= linq.OrderByDescending(x=>x.CrearTime).ToList();
            paging.Code = ApiEnum.OK;
            paging.Msg = "获取角色列表";
            return Task.FromResult(paging);
            
        }
    }
}
